package com.csw.shuanfa.SuanFa;

import java.util.HashSet;
import java.util.Set;

/**
 * 3 输入一个没有重复字符的字符串，打印出该字符串中所有字符的排列。如输入abc，则打印出a、b、c所能排列出来的所有字符串abc, acb, bac, bca, cab, cba。用Java/JS/TS/C++语言写一个函数实现。
 * 要求：
 * (1)命名一个合适的函数名称，实现完整的函数
 * (2)仅用一个函数实现，不要出现调用子函数
 * (3) 按照你平时的编程习惯，在你认为需要注释处写上相应的注释
 */
public class TestQPL {

    private static Set set;
    private static char[] chars;
    private static Object[] objects;

    public static void main(String[] args) {
        String s = "abcde";
        Set set = new HashSet();
        set.add(s);
        set = change(0, set);
        //set.toString());
    }

    private static Set change(int index, Set set) {
        objects = set.toArray();
        for (int m = 0; m < objects.length; m++) {
            String aa = (String) objects[m];
            chars = aa.toCharArray();
            for (int i = index + 1; i < chars.length; i++) {
                char temp = chars[index];
                chars[index] = chars[i];
                chars[i] = temp;

                StringBuffer sb = getStringBuffer();
                set.add(sb.toString());
            }
        }

        if (index <= chars.length - 2) {
            index = index + 1;
            change(index, set);
        }
        return set;
    }

    private static StringBuffer getStringBuffer() {
        StringBuffer sb = new StringBuffer();
        for (int n = 0; n < chars.length; n++) {
            sb.append(chars[n]);
        }
        return sb;
    }
}
